/* 1、判断是否VIP，不是，启动定时器函数，
 * 2、检查本地是否有存储，没有则从服务器拉取
 * 3、启动定时器循环
 * 
 * */ 
 
//plus.storage.removeItem('MsgReplyData');
 
var reply = {
	stringify:plus.storage.getItem('MsgReplyData'),
	replydata:[],
	data:[],
	timestamp:0
}
if(reply.stringify){
	setreply(JSON.parse(reply.stringify));
}
function setreply(arr){
	reply.replydata = arr;
	reply.data = reply.replydata.data;
	reply.timestamp = reply.replydata.timestamp;
	
}
var WebId_My = plus.webview.getWebviewById('My');
var timeid;
var DingTimeReply = {
	Initialization:function(){
		if(!isallpay()) return;
		msgreply = true;
		if(!isvipcoin(false)) {
			clearTimeout(timeid);
			DingTimeReply.setreply(false);
		}
	}, 
	over:function(){
		clearTimeout(timeid);
	}, 
	settime:function(ktime){
		if(isvipcoin(false)) return;
		var jg = Math.abs(ktime)*1000;
		timeid = setTimeout(function(){
			reply.data = reply.replydata.data;
			DingTimeReply.setreply(true);
		},jg)
	},
	setreply:function(type){
		if(!msgreply){
			return;
		}
		var timestamp = (Date.parse(new Date())) / 1000;
		mui.each(reply.data,function(index,value){
			if(timestamp>=value.send_time){
				if(reply.data[index].reply_type == 9){
					DingTimeReply.postLF(value);
				}else if(reply.data[index].reply_type == 10){
					DingTimeReply.postGZ(value);
				}else{
					DingTimeReply.postMsg(value,type);
				}
//				if(reply.data[index].reply_type != 5){
					reply.replydata.data.splice(index,1);
//				}
				
			}else{
				plus.storage.setItem('MsgReplyData',JSON.stringify(reply.replydata));
				var tt = value.send_time -timestamp;
				DingTimeReply.settime(tt);
				return false;
			}
			plus.storage.setItem('MsgReplyData',JSON.stringify(reply.replydata));
		})
		if(reply.data.length==0){
			this.getreplydata();
			return;
		}
	},
	getreplydata: function() {
		mui.getJSON(getApiUrl('http://www.jiaoyou0.cn','Userdata/replydata'), {
			page: 1
		}, function(data) {
			if(data.code ==200){ 
				//初始化参数
				setreply(data);
				//存储
				plus.storage.setItem('MsgReplyData',JSON.stringify(data));
				DingTimeReply.setreply(true);
			}else{
				//关闭定时器
				clearTimeout(timeid);
			}
		});
	},
	postMsg: function(val,type) {
		val['send_uid'] = val.user_id;
		val['yuedu'] = 1;
		dojishicall(val,type,false);
	},
	postLF: function(val) {
		laifang(val);
	},
	postGZ: function(val) {
		var fans = new Array();
		var fs = plus.storage.getItem('My_fans');
		if(fs){
			fans = JSON.parse(fs);
		}
		fans.push({user_id:val.user_id,nick_name:val.nick_name,headimg:val.headimg});
		plus.storage.setItem('My_fans', JSON.stringify(fans));
		mui.fire(WebId_My, 'GZfans');
	}
}

DingTimeReply.Initialization();